require "common/Define"
require "framework/BaseController"
require "FairyGUI"

ScrollPaneMain = BaseController:New({mControllerName = "ScrollPaneMain"});

function ScrollPaneMain.Main()
	-- body
	ScrollPaneMain:Awake();
end

function ScrollPaneMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/ScrollPane";
	UIPackage.AddPackage(packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("ScrollPane", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	--local go = GameObject();
	--local panel = go:AddComponent(typeof(UIPanel));
	--panel.container:AddChild(self.mainComponent);
	--GRoot.inst:AddChild( panel );

	-- self.mainComponent:SetSize(GRoot.inst.width, GRoot.inst.height);
	-- self.mainComponent:AddRelation(GRoot.inst, RelationType.Size);
	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);
end

function ScrollPaneMain:InitView()
	self._list = self.mainComponent:GetChild("list").asList;
	self._list.itemRenderer = FairyGUI.ListItemRenderer(self.RenderListItem, self);
	self._list:SetVirtual();
	self._list.numItems = 1000;
	self._list.onTouchBegin:Add(self.OnClickList, self);

	self.mainComponent:GetChild("box").asCom.onDrop:Add(self.OnDrop, self);

	print(FairyGUI.LongPressGesture);
	local gesture = FairyGUI.LongPressGesture(self._list);
	gesture.once = true;
	gesture.trigger = 1;
	gesture.onAction:Add(self.OnLongPress, self);
end

function ScrollPaneMain:RenderListItem(index, obj)
	local item = obj.asButton;
	item.title = "Item " .. index;
	item.data = "Item" .. index;
	item.name = "Item" .. index;
	item.text = "Item" .. index;

	print("item_", index, ": ", item.data);
	print(index, obj, item, item.title, self);
	item.scrollPane.posX = 0; --reset scroll pos

	--Be carefull, RenderListItem is calling repeatedly, dont call 'Add' here!
	-- 请注意，RenderListItem是重复调用的，不要使用Add增加侦听！
	item:GetChild("b0").onClick:Set(self.OnClickStick, self);
	item:GetChild("b1").onClick:Set(self.OnClickDelete, self);
end	

function ScrollPaneMain:OnClickList(context)
	-- find out if there is an item in edit status
	-- 查找是否有项目处于编辑状态
	local cnt = self._list.numChildren;
	for i=0, cnt-1,1 do
		local item = self._list:GetChildAt(i).asButton;
		if (item.scrollPane.posX ~= 0) then
			-- Check if clicked on the button
			if (item:GetChild("b0").asButton:IsAncestorOf(GRoot.inst.touchTarget) or item:GetChild("b1").asButton:IsAncestorOf(GRoot.inst.touchTarget)) then
				return;
			end

			item.scrollPane:SetPosX(0, true);
			--avoid scroll pane default behavior
			--取消滚动面板可能发生的拉动。
			item.scrollPane:CancelDragging();
			self._list.scrollPane:CancelDragging();

			break;
		end
	end
end	

function ScrollPaneMain:OnLongPress(context)
	--find out which item is under finger
	--逐层往上知道查到点击了那个item
	local obj = GRoot.inst.touchTarget;
	local p = obj.parent;

	while (p ~= null) do
		if (p == self._list) then
			break;
		end

		p = p.parent;
	end

	if (p == null) then return; end

	print("LongPress: target=", obj, obj.text, ", list: ", self._list);
	FairyGUI.DragDropManager.inst:StartDrag(obj, obj.icon, obj.parent.text);
end

function ScrollPaneMain:OnDrop(context)
	print("----------------------------------------------------");
	print("OnDrop: ", context, self);
	print("context: ", context);
	print("context.data", context.data);
	print("context.sender: ", context.sender);
	print("context.sender.name: ", context.sender.name);

	self.mainComponent:GetChild("txt").text = "Drop " .. context.data;
end

function ScrollPaneMain:OnClickStick(context)
	ScrollPaneMain.mainComponent:GetChild("txt").text = "Stick " .. ((context.sender).parent).text;
end

function ScrollPaneMain:OnClickDelete(context)
	ScrollPaneMain.mainComponent:GetChild("txt").text = "Delete " .. ((context.sender).parent).text;
end

function ScrollPaneMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end

